﻿using System;
using System.Security;
using System.Security.Principal;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.Mvc;
using System.Web.Routing;
using CMS.Web.Engine;
using CMS.Web.Data;

namespace CMS.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*path}");
            routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

            routes.MapRoute("WidgetEdit", "widget/edit/{id}",
                new { controller = "Widget", action = "Edit" });

            routes.MapRoute("WidgetDelete", "widget/delete/{id}",
                new { controller = "Widget", action = "Delete" });

            routes.MapRoute("WidgetAdd", "widget/add",
                new { controller = "Widget", action = "Add" });           

            routes.MapRoute("PageLayout", "page/layout/{id}",
                new { controller = "Layout", action = "Index" });

            routes.MapRoute("Index", "{*path}",
                new { controller = "Page", action = "Index" });

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            IoCContainer.Current.RegisterType<IPagesRepository, PagesRepository>(LifetimeManagement.ContainerControlled);
            IoCContainer.Current.RegisterType<IHostsRepository, HostsRepository>(LifetimeManagement.ContainerControlled);
            IoCContainer.Current.RegisterType<IUsersRepository, UsersRepository>(LifetimeManagement.ContainerControlled);
            
            IControllerFactory factory = new ControllerFactory();
            ControllerBuilder.Current.SetControllerFactory(factory); 
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
            if (authCookie != null)
            {               
                FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);              
                string[] roles = authTicket.UserData.Split("|".ToCharArray());               
                IIdentity id = new FormsIdentity(authTicket);               
                IPrincipal principal = new GenericPrincipal(id, roles);   
          
                Context.User = principal;
            }
        }
    }
}
